package com.ipanel.layout2dp;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;

/**
 * 
 * @author Zexu
 *
 */
public class LayoutConvert2dp {

	public static void main(String[] args) {
		float scale = 1f;

		// folder or file to be converted
		File f = new File("D:\\workspace\\HuNanHomed_WS\\res\\layout");

		// output directory
		File out = new File("out");

		try {
			if (!out.exists())
				out.mkdirs();
			if (f.isDirectory()) {
				File[] files = f.listFiles();
				for (File fl : files) {
					if (fl.isFile()) {
						System.out.println("process: " + fl.getName());
						convertFile(scale, out, fl);
					}
				}
			} else {
				convertFile(scale, out, f);
			}
			// System.out.println(convertXmlContent(test, 1.5f));
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public static void convertFile(float scale, File out, File fl)
			throws IOException, FileNotFoundException, Exception {
		// String content = new String(IS2ByteArray(new FileInputStream(fl)));
		BufferedReader reader = new BufferedReader(new FileReader(fl));
		File fo = new File(out, fl.getName());
		BufferedWriter writer = new BufferedWriter(new FileWriter(fo));
		String line = null;
		boolean isFirst = true;
		while ((line = reader.readLine()) != null) {
			if (!isFirst) {
				writer.newLine();
			} else {
				isFirst = false;
			}
			writer.write(convertXmlContent(line, scale));
		}
		reader.close();
		writer.flush();
		writer.close();
	}

	public static byte[] IS2ByteArray(InputStream is) throws IOException {
		BufferedInputStream bis = new BufferedInputStream(is);
		byte[] buf = new byte[8192];
		ByteArrayOutputStream bao = new ByteArrayOutputStream();
		int len;
		while ((len = bis.read(buf)) != -1) {
			bao.write(buf, 0, len);
		}
		return bao.toByteArray();
	}

	public static String convertXmlContent(String content, float scale)
			throws Exception {
		if (content.contains("android:id") || content.contains("android:text=")
				|| content.contains("@drawable") || content.contains("@color")
				|| content.contains("@string")) {
			System.out.print("skip " + content);
			return content;
		}
		if (!content.contains("px\""))
			return content;
		content = content.replace("px\"", "dp\"");
		// content = content.replace("sp\"", "px\"");

		StringBuffer buffer = new StringBuffer();
		String end = "dp\"";
		int idx = content.indexOf(end);
		if (idx == -1) {
			end = "dp<";
			idx = content.indexOf(end);
		}
		while (idx != -1) {
			String sub = content.substring(0, idx);
			int i0 = sub.lastIndexOf('"');
			if ("dp<".equals(end))
				i0 = sub.lastIndexOf('>');
			if (i0 == -1)
				throw new Exception("missing begin double quote");
			buffer.append(sub.substring(0, i0 + 1));
			float val = Float.parseFloat(sub.substring(i0 + 1));
			val = Math.round(scale * val * 10);
			val /= 10;
			// if("px<".equals(end))
			// buffer.append('>');
			// else
			// buffer.append('"');
			buffer.append(val);
			buffer.append(end);

			content = content.substring(idx + 3);

			idx = content.indexOf("dp\"");
			if (idx == -1) {
				end = "dp<";
				idx = content.indexOf(end);
			}
		}
		buffer.append(content);
		return buffer.toString();
	}
}
